#include "StdAfx.h"
#include ".\log.h"
#include "time.h"
#include "CriticalSection.h"
#include <memory>
#include <iostream>
#include <fstream>

namespace Lumin {
namespace Win32 {
CCriticalSection s_criticalSection;

static fstream s_debugOut;
static std::string s_logFileName = "lumin.log";

void SetLogFileName(const  std::string  &name)
{
	if (s_debugOut.is_open()) {
		s_debugOut.close();
	}
	s_logFileName = name;
}

void Output(const char* fmt, ...)
{
	char buffer[1024 * 4];

	va_list args;
	va_start( args, fmt );
	vsprintf( buffer, fmt, args);
	SYSTEMTIME lpTime;
	GetLocalTime(&lpTime);
	char logTime[20];
	sprintf(logTime, "%d-%d-%d %d:%d:%d", lpTime.wYear, lpTime.wMonth, lpTime.wDay, lpTime.wHour, lpTime.wMinute, lpTime.wSecond);
	char szBuffer[1024 * 5];
	wsprintf(szBuffer,"[%s] %s", logTime, buffer);

	CCriticalSection::Owner lock(s_criticalSection);
	if (!s_debugOut.is_open())
	{
		s_debugOut.open(s_logFileName.c_str(), std::ios_base::out | std::ios_base::app);
		s_debugOut << _T("****************New Log*****************") << endl;
	}
	s_debugOut << szBuffer << endl;
}

}
}